//
//  PKViewController.m
//  ShrimpCrowdordering
//
//  Created by 郑洲 on 2018/7/31.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "PKViewController.h"
#import "PKDetailViewController.h"
#import "PKListViewController.h"

@interface PKViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) UIImageView *myImage;
@property (nonatomic, assign) NSInteger selectIndex;
@property (nonatomic, strong) NSArray *dataArray;

@end

@implementation PKViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"PK";
    self.selectIndex = 0;
    _dataArray = [NSMutableArray array];
    
    UIButton *listBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, ScreenX375(50), ScreenX375(21))];
    [listBtn setTitle:@"PK记录" forState:UIControlStateNormal];
    [listBtn setTitleColor:REXADECIMALCOLOR(@"#222222") forState:UIControlStateNormal];
    listBtn.titleLabel.font = Kfont(14);
    [listBtn addTarget:self action:@selector(list) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:listBtn];
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - ScreenX375(49) - NAVIGATION_BAR_HEIGHT)];
    _scrollView.contentSize = CGSizeMake(0, ScreenX375(949));
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_scrollView];
    
    UIButton *pkBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - ScreenX375(49) - NAVIGATION_BAR_HEIGHT, SCREEN_WIDTH, ScreenX375(49))];
    [pkBtn setTitle:@"确定" forState:UIControlStateNormal];
    [pkBtn setBackgroundImage:ImageWithName(@"按钮背景") forState:UIControlStateNormal];
    [pkBtn setTitleColor:REXADECIMALCOLOR(@"#ffffff") forState:UIControlStateNormal];
    pkBtn.titleLabel.font = Kfont(16);
    [pkBtn addTarget:self action:@selector(pkAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:pkBtn];
    
    [self setTopUI];
    [self setBottomUI];
    [self getData];
}

- (void)list {
    PKListViewController *vc = [[PKListViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)setTopUI {
    UIImageView *otherView = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(37), ScreenX375(385), SCREEN_WIDTH - ScreenX375(74), ScreenX375(300))];
    [otherView sd_setImageWithURL:[NSURL URLWithString:_model.proPic] placeholderImage:nil options:SDWebImageRetryFailed];
    otherView.layer.cornerRadius = ScreenX375(6);
    otherView.clipsToBounds = YES;
    otherView.contentMode = UIViewContentModeScaleAspectFill;
    [_scrollView addSubview:otherView];
    
    CGFloat otherNameWidth = kGetStringWidth(_model.nickName, CGSizeMake(ScreenX375(150), ScreenX375(16)), KBlodfont(14));
    UIView *otherDetail = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(14), ScreenX375(14), ScreenX375(72) + otherNameWidth, ScreenX375(36))];
    otherDetail.layer.cornerRadius = ScreenX375(18);
    otherDetail.layer.shadowOffset = CGSizeMake(0, 2); //设置阴影的偏移量
    otherDetail.layer.shadowRadius = ScreenX375(8);  //设置阴影的半径
    otherDetail.layer.shadowColor = RGBACOLOR(0, 0, 0, 0.09).CGColor; //设置阴影的颜色为黑色
    otherDetail.layer.shadowOpacity = 1; //设置阴影的不透明度
    otherDetail.backgroundColor = [UIColor whiteColor];
    [otherView addSubview:otherDetail];
    
    UIImageView *otherHead = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(12), ScreenX375(4), ScreenX375(28), ScreenX375(28))];
    otherHead.layer.cornerRadius = ScreenX375(14);
    otherHead.clipsToBounds = YES;
    [otherHead sd_setImageWithURL:[NSURL URLWithString:_model.userPic] placeholderImage:nil options:SDWebImageRetryFailed];
    [otherDetail addSubview:otherHead];
    
    UILabel *otherName = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(50), ScreenX375(10), otherNameWidth + ScreenX375(2), ScreenX375(16))];
    otherName.text = _model.nickName;
    otherName.textColor = REXADECIMALCOLOR(@"#f21934");
    otherName.font = KBlodfont(14);
    [otherDetail addSubview:otherName];
    
    UILabel *vsLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - ScreenX375(12), ScreenX375(340), ScreenX375(24), ScreenX375(25))];
    vsLabel.text = @"VS";
    vsLabel.textAlignment = NSTextAlignmentCenter;
    vsLabel.font = Kfont(18);
    vsLabel.textColor = FONTCOLOR_BLACK;
    [_scrollView addSubview:vsLabel];
    
    self.myImage = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(37), ScreenX375(20), SCREEN_WIDTH - ScreenX375(74), ScreenX375(300))];
    self.myImage.layer.cornerRadius = ScreenX375(8);
    self.myImage.clipsToBounds = YES;
    self.myImage.contentMode = UIViewContentModeScaleAspectFill;
    self.myImage.backgroundColor = RGB_COLOR(244, 244, 244);
    [_scrollView addSubview:self.myImage];
    
    CGFloat myNameWidth = kGetStringWidth(kNSUDefaultReadKey(@"nickName"), CGSizeMake(ScreenX375(150), ScreenX375(16)), KBlodfont(14));
    UIView *myDetail = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(14), ScreenX375(14), ScreenX375(72) + myNameWidth, ScreenX375(36))];
    myDetail.layer.cornerRadius = ScreenX375(18);
    myDetail.layer.shadowOffset = CGSizeMake(0, 2); //设置阴影的偏移量
    myDetail.layer.shadowRadius = ScreenX375(8);  //设置阴影的半径
    myDetail.layer.shadowColor = RGBACOLOR(0, 0, 0, 0.09).CGColor; //设置阴影的颜色为黑色
    myDetail.layer.shadowOpacity = 1; //设置阴影的不透明度
    myDetail.backgroundColor = [UIColor whiteColor];
    [self.myImage addSubview:myDetail];
    
    UIImageView *myHead = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(12), ScreenX375(4), ScreenX375(28), ScreenX375(28))];
    myHead.layer.cornerRadius = ScreenX375(14);
    myHead.clipsToBounds = YES;
    [myHead sd_setImageWithURL:[NSURL URLWithString:kNSUDefaultReadKey(@"pic")] placeholderImage:nil options:SDWebImageRetryFailed];
    [myDetail addSubview:myHead];
    
    UILabel *myName = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(50), ScreenX375(10), myNameWidth + ScreenX375(2), ScreenX375(16))];
    myName.text = kNSUDefaultReadKey(@"nickName");
    myName.textColor = REXADECIMALCOLOR(@"#4a90e2");
    myName.font = KBlodfont(14);
    [myDetail addSubview:myName];
}

- (void)getData {
    [HttpManager postHttpRequestByGet:[NSString stringWithFormat:@"%@api/getProsByPk",REQUESTHEADER] andParameter:@{@"userId":kNSUDefaultReadKey(@"userId")} success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"code"]];
        if ([status isEqualToString:@"200"]) {
            self.dataArray = successResponse[@"data"][@"list"];
            if (self.dataArray.count > 0) {
                [self.myImage sd_setImageWithURL:[NSURL URLWithString:self.dataArray[self.selectIndex][@"pic"]] placeholderImage:nil options:SDWebImageRetryFailed];
            }
            [self.collectionView reloadData];
        }else {
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}

- (void)setBottomUI {
    UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(37), ScreenX375(725), SCREEN_WIDTH - ScreenX375(48), ScreenX375(18))];
    noteLabel.text = @"选择我的作品";
    noteLabel.font = KBlodfont(16);
    noteLabel.textColor = REXADECIMALCOLOR(@"#212121");
    [_scrollView addSubview:noteLabel];
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.minimumInteritemSpacing = ScreenX375(24);
    flow.minimumLineSpacing = ScreenX375(16);
    flow.sectionInset = UIEdgeInsetsMake(0, ScreenX375(37), 0, 0);
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flow.itemSize = CGSizeMake(ScreenX375(150), ScreenX375(150));
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, ScreenX375(758), SCREEN_WIDTH, ScreenX375(150)) collectionViewLayout:flow];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor clearColor];
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"dataCell"];
    [_scrollView addSubview:_collectionView];
}

- (void)pkAction {
    if (_dataArray.count == 0) {
        [HttpManager showNoteMsg:@"暂无作品进行PK，请先发布作品"];
        return;
    }
    if (_model == nil) {
        return;
    }
    [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@api/PKPro",REQUESTHEADER] andParameter:@{@"initiator":kNSUDefaultReadKey(@"userId"),@"acceptProId":_model.proId,@"initiatProId":_dataArray[_selectIndex][@"proId"]} success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"code"]];
        if ([status isEqualToString:@"200"]) {
            PKDetailViewController *vc = [[PKDetailViewController alloc] init];
            vc.pkId = [NSString stringWithFormat:@"%@",successResponse[@"data"][@"id"]];
            [self.navigationController pushViewController:vc animated:YES];
        }else {
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * CellIdentifier = @"dataCell";
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    
    for (UIView *subView in cell.subviews) {
        [subView removeFromSuperview];
    }
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenX375(147), ScreenX375(136))];
    if ([_dataArray[indexPath.item][@"pic"] class] != [NSNull class]) {
        [imageView sd_setImageWithURL:[NSURL URLWithString:_dataArray[indexPath.item][@"pic"]] placeholderImage:nil options:SDWebImageRetryFailed];
    }
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.clipsToBounds = YES;
    imageView.layer.cornerRadius = ScreenX375(8);
    [cell addSubview:imageView];
    
    UIButton *selectView = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(120), ScreenX375(6), ScreenX375(21), ScreenX375(21))];
    [cell addSubview:selectView];
    
    if (_selectIndex == indexPath.item) {
        [selectView setBackgroundImage:[UIImage imageNamed:@"椭圆2"] forState:UIControlStateNormal];
        [selectView setImage:[UIImage imageNamed:@"打钩"] forState:UIControlStateNormal];
    }else {
        [selectView setBackgroundImage:[UIImage imageNamed:@"椭圆1拷贝8"] forState:UIControlStateNormal];
        [selectView setImage:nil forState:UIControlStateNormal];
    }
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    _selectIndex = indexPath.item;
    [self.myImage sd_setImageWithURL:[NSURL URLWithString:self.dataArray[_selectIndex][@"pic"]] placeholderImage:nil options:SDWebImageRetryFailed];
    [_collectionView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
